Skip to content

refactor: 超长文件拆分 + 函数分解 + 调度工具提取#2519

Open
Michael-Jetson wants to merge 2 commits into
Wei-Shaw:mainfrom
Michael-Jetson:refactor/function-decomposition
Open

refactor: 超长文件拆分 + 函数分解 + 调度工具提取#2519
Michael-Jetson wants to merge 2 commits into
Wei-Shaw:mainfrom
Michael-Jetson:refactor/function-decomposition

Conversation

@Michael-Jetson
Copy link
Copy Markdown
Contributor

@Michael-Jetson Michael-Jetson commented May 16, 2026

概述

纯结构重构,不改变任何行为。将最大文件缩减 50-62%,最复杂函数从 654 行分解为 25 行调度器。

改动内容

文件拆分(37 个新文件):

文件 重构前 重构后 缩减
gateway_service.go 9,579 3,300 -66%
openai_gateway_service.go 6,397 3,822 -40%
openai_ws_forwarder.go 4,228 2,416 -43%
admin_service.go 3,698 2,051 -45%
usage_log_repo.go 4,394 1,682 -62%
setting_service.go 4,032 2,502 -38%

函数分解:

  • SelectAccountWithLoadAwareness(654行)→ 7 个子方法,通过 gateway_account_selection_helpers.go 实现
  • Forward(Gateway, 620行)→ 提取 prepareForwardBody + processForwardResponse

共享工具(gateway_scheduling_utils.go):

  • isAccountEligibleForScheduling — 统一 7 项资格检查(消除 8 处重复)
  • sortAccountsWithLoadByPriority — 统一负载感知排序 + 组内随机
  • isBetterAccountCandidate — 统一优先级→LRU→OAuth 比较(消除 5 处重复)

不变的内容

  • 零行为变更 — 所有函数签名保持不变
  • 未添加新依赖
  • 未修改 API 契约
  • 未修改配置格式

测试计划

  • go build ./... 编译通过
  • 全部 43 个单元测试包通过
  • 竞态检测通过
  • CI 流水线验证

🤖 Generated with Claude Code

Structural refactoring to improve maintainability without changing any behavior:

**Phase 1 - File splitting (37 new files):**
- gateway_service.go: 9,579 → 3,300 lines (split into 15 focused files)
- openai_gateway_service.go: 6,397 → 3,822 lines (6 new files)
- openai_ws_forwarder.go: 4,228 → 2,416 lines (5 new files)
- admin_service.go: 3,698 → 2,051 lines (5 new files)
- usage_log_repo.go: 4,394 → 1,682 lines (3 new files)
- setting_service.go: 4,032 → 2,502 lines (3 new files)

**Phase 2 - Function decomposition:**
- SelectAccountWithLoadAwareness (654 lines → 25-line dispatcher + 7 sub-methods)
  New: gateway_account_selection_helpers.go with schedulingContext/accountPool structs
- Forward (Gateway, 620 lines → 380 lines)
  New: gateway_forward_helpers.go with prepareForwardBody/processForwardResponse

**DRY optimization:**
- New: gateway_scheduling_utils.go
  - isAccountEligibleForScheduling: unified 7-check gate (eliminates 8 repetitions)
  - sortAccountsWithLoadByPriority: unified load-aware sort+shuffle
  - isBetterAccountCandidate: unified priority→LRU→OAuth comparison (eliminates 5 repetitions)

**Verification:**
- All 43 test packages pass
- Zero behavior change (function signatures preserved)
- Build passes with Go 1.26.3

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@Michael-Jetson Michael-Jetson changed the title refactor: decompose mega-files and extract shared scheduling utilities refactor: 超长文件拆分 + 函数分解 + 调度工具提取 May 16, 2026
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant